package cn.wolfcode.luowowo.website_api.controller;

import cn.wolfcode.luowowo.QueryObject.StrategyCommentQuery;
import cn.wolfcode.luowowo.QueryObject.StrategyQuery;
import cn.wolfcode.luowowo.annotation.RequireLogin;
import cn.wolfcode.luowowo.annotation.UserParam;
import cn.wolfcode.luowowo.domain.*;
import cn.wolfcode.luowowo.redis.redisService.IStrategyFavorRedisService;
import cn.wolfcode.luowowo.redis.redisService.IStrategyStatisVoService;
import cn.wolfcode.luowowo.service.*;
import cn.wolfcode.luowowo.util.CommentQueryUtil;
import cn.wolfcode.luowowo.util.GetToast;
import cn.wolfcode.luowowo.util.JsonResult;
import cn.wolfcode.luowowo.util.ParamMap;
import cn.wolfcode.luowowo.vo.FavorStrategyVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/strategy")
public class StrategyController {

    @Autowired
    private IStrategyCatalogService strategyCatalogService;
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyCommentService strategyCommentService;
    @Autowired
    private IStrategyThemeService strategyThemeService;
    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private IStrategyStatisVoService strategyStatisVoService;
    @Autowired
    private IStrategyFavorRedisService strategyFavorRedisService;


    @RequestMapping("/queryCatalogsByDestId")
    @GetMapping
    public Object queryCatalogsByDestId(String destId) {

        //catalogs
        List<StrategyCatalog> strategyCatalogs = strategyCatalogService.getCatalogByDsetId(destId);
        return JsonResult.success(strategyCatalogs);
    }

    @GetMapping("/detail")
    public Object detail(String id, @UserParam UserInfo userInfo) {
        //strategy
        Strategy strategy = null;
        if (StringUtils.hasLength(id)) {
            strategy = strategyService.get(id);
        }
        //攻略评论分页查询
        StrategyCommentQuery qo = new StrategyCommentQuery();
        qo.setStrategyId(id);
        Page<StrategyComment> page = strategyCommentService.query(qo);

        //点击数viewnum 加一
        strategyStatisVoService.viewnumIncrease(id, 1);

        //若登录了 才返回当前用户已收藏攻略id集合
        List<String> list = Collections.EMPTY_LIST;
        if (userInfo != null) {
            FavorStrategyVO favorStrategyVO = strategyFavorRedisService.getFavorRedis(userInfo.getId());
            if(favorStrategyVO!=null){
                list = favorStrategyVO.getFavorStrategyIds();
            }
        }
        return JsonResult.success(new ParamMap()
                .put("strategy", strategy)
                .put("page", page)
                .put("sids", list)
                .put("vo", strategyStatisVoService.getStrategyStatisVo(id))
        );
    }

    //在具体目的地下 查看所有攻略
    @GetMapping("/list")
    public Object list(StrategyQuery qo) {
        //page
        Page<Strategy> page = strategyService.query(qo);

        //theme
        List<StrategyTheme> themes = strategyThemeService.list();

        //toasts
        List<Destination> toast = null;
        if (StringUtils.hasLength(qo.getDestId())) {
            toast = GetToast.getToastByDestId(qo.getDestId(), destinationService);
        }
        return JsonResult.success(new ParamMap()
                .put("themes", themes)
                .put("toast", toast)
                .put("page", page));
    }

    @GetMapping("/query")
    public Object query(StrategyQuery qo) {
        //实现高级查询 可以根据攻略主题 目的地查询
        Page<Strategy> page = strategyService.query(qo);
        return JsonResult.success(page);
    }

    @PostMapping("/addComment")
    @RequireLogin
    public Object addComment(StrategyComment strategyComment, @UserParam UserInfo userInfo) {
        //1.维护评论中的用户信息
        BeanUtils.copyProperties(userInfo, strategyComment);
        strategyComment.setUserId(userInfo.getId());
        //2.添加评论
        strategyCommentService.addComment(strategyComment);
        //3.评论数 +1
        strategyStatisVoService.replynumIncrease(strategyComment.getStrategyId(), 1);
        //4.攻略评论分页查询 并返回page给前端
        Page<StrategyComment> page = CommentQueryUtil.
                StrategyCommentQueryByStrategyId(strategyComment.getStrategyId(), strategyCommentService);

        return JsonResult.success(new ParamMap()
                .put("page", page)
                .put("vo", strategyStatisVoService.getStrategyStatisVo(strategyComment.getStrategyId()))
        );
    }

    @PostMapping("/commentThumb")
    @RequireLogin
    //cid : CommentId   sid : strategyId
    public Object commentThumb(String cid, String sid, @UserParam UserInfo userInfo) {
        //1.点赞 / 取消点赞
        strategyCommentService.commentThumb(cid, userInfo.getId());
        //2.查询评论分页
        StrategyCommentQuery qo = new StrategyCommentQuery();
        qo.setStrategyId(sid);
        Page<StrategyComment> page = strategyCommentService.query(qo);
        return JsonResult.success(page);
    }

    //攻略评论的分页查询
    @GetMapping("/commentList")
    @RequireLogin
    public Object commentList(String strategyId, int currentPage) {

        //分页查询
        StrategyCommentQuery qo = new StrategyCommentQuery();
        qo.setStrategyId(strategyId);
        qo.setCurrentPage(currentPage);
        Page<StrategyComment> page = strategyCommentService.query(qo);
        return JsonResult.success(page);
    }

    //攻略收藏或取消收藏操作
    @PostMapping("/favor")
    @RequireLogin
    //sid: 攻略id
    public Object favor(String sid, @UserParam UserInfo userInfo) {

        //1.收藏 或 取消收藏操作
        boolean ret = strategyStatisVoService.favor(sid, userInfo.getId());
        //2.根据用户id 获取缓存中的 已收藏攻略id结合
        FavorStrategyVO favorStrategyVO = strategyFavorRedisService.getFavorRedis(userInfo.getId());
        List<String> favorStrategyIds = favorStrategyVO.getFavorStrategyIds();
        return JsonResult.success(new ParamMap()
                .put("ret", ret)
                .put("vo", strategyStatisVoService.getStrategyStatisVo(sid))
                .put("sids", favorStrategyIds)
        );
    }

    //攻略点赞操作 顶攻略
    @PostMapping("/strategyThumbup")
    @RequireLogin
    //sid: 攻略id
    public Object strategyThumbup(String sid, @UserParam UserInfo userInfo) {

        boolean ret = strategyStatisVoService.thumbup(sid, userInfo.getId());

        return JsonResult.success(new ParamMap()
                .put("ret", ret)
                .put("vo", strategyStatisVoService.getStrategyStatisVo(sid))
        );
    }


}
